plotly provides a platform for online data analytics and visualizations; build on of HTML etc.; “grammar of graphics” in ggplot; convey a lot of info in a easy-to-understand format.
library(tidyverse)
library(plotly)
library(p8105.datasets)
Lets load the Airbnb datsets.
data("nyc_airbnb")
nyc_airbnb=nyc_airbnb|>
mutate(
rating=review_scores_location/2
)|>
select(
neighbourhood_group, neighbourhood, rating, price, room_type, lat, long
)|>
filter(
neighbourhood_group=="Manhattan",
room_type=="Entire home/apt",
price <= 500,
price >=100
)
nyc_airbnb
## # A tibble: 9,535 × 7
## neighbourhood_group neighbourhood rating price room_type lat long
## <chr> <chr> <dbl> <dbl> <chr> <dbl> <dbl>
## 1 Manhattan Battery Park City NA 400 Entire home/a… -74.0 40.7
## 2 Manhattan Battery Park City NA 225 Entire home/a… -74.0 40.7
## 3 Manhattan Battery Park City NA 220 Entire home/a… -74.0 40.7
## 4 Manhattan Battery Park City 4.5 165 Entire home/a… -74.0 40.7
## 5 Manhattan Battery Park City NA 350 Entire home/a… -74.0 40.7
## 6 Manhattan Battery Park City NA 400 Entire home/a… -74.0 40.7
## 7 Manhattan Battery Park City NA 130 Entire home/a… -74.0 40.7
## 8 Manhattan Battery Park City 5 225 Entire home/a… -74.0 40.7
## 9 Manhattan Battery Park City NA 245 Entire home/a… -74.0 40.7
## 10 Manhattan Battery Park City NA 150 Entire home/a… -74.0 40.7
## # ℹ 9,525 more rows
Let’s make a scatterplot but interactive at this time.
nyc_airbnb |>
mutate(
label=str_c("Price:&", price, "\nRating: ", rating)
)|>
plot_ly(
x = ~lat, y=~long, color=~price,
text=~label,
type = "scatter", mode = "markers", alpha=.5
)
lets make a boxplot
nyc_airbnb |>
mutate(
neighbourhood=fct_reorder(neighbourhood,price)) |>
plot_ly(
x=~neighbourhood, y=~price,color=~neighbourhood,
type="box", colors = "viridis"
)
barcharts
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(x = ~neighbourhood, y = ~n, color = ~neighbourhood, type = "bar", colors = "viridis")
do not use this often!
ggp_scatterplot=
nyc_airbnb|>
ggplot(aes(x=lat, y=long, color=price))+
geom_point()
ggplotly(ggp_scatterplot)